/**
 * 获取 起始日期范围内的所有时间（包含起始时间）
 * @param startNyrDateStr 起始日期（格式：年月日）
 * @param endNyrDateStr 起始日期（格式：年月日）
 * @returns {Array<string>}
 * @example
 *  const res1=getTimespan('2023-01-01,'2023-01-03')
 *  console.log(res1) //输出：['2023-01-01,'2023-01-02','2023-01-03']
 *
 *  const res2=getTimespan('2023-01-01')
 *  console.log(res2) //输出：['2023-01-01']
 */
function getDatespanStr(startNyrDateStr, endNyrDateStr) {
  const startNyrDate = new Date(startNyrDateStr);
  const endNyrDate = new Date(endNyrDateStr);

  const dates = [];
  const currentDate = startNyrDate;

  //可比较大小
  while (currentDate <= endNyrDate) {
    dates.push(dateFormat(currentDate).split(" ")[0]);
    currentDate.setDate(currentDate.getDate() + 1);
  }

  return dates;
}

/**
 * 格式化时间的函数
 * @param {Date} date
 * @return {string} 格式：YYYY-MM-DD HH:mm:SS
 */
function dateFormat(date) {
  let format = "YYYY-MM-DD HH:mm:SS";
  const config = {
    YYYY: date.getFullYear(),
    MM: date.getMonth() + 1,
    DD: date.getDate(),
    HH: date.getHours(),
    mm: date.getMinutes(),
    SS: date.getSeconds(),
  };
  for (const key in config) {
    format = format.replace(key, config[key]);
  }
  return format;
}

/**
 * 返回季度信息
 * 例子：
 * {
 *       '2021Q4': [ '2021-10-1', '2021-12-31' ],
 *       '2022Q1': [ '2022-1-1', '2022-3-31' ],
 *       '2022Q2': [ '2022-4-1', '2022-6-30' ],
 *       '2022Q3': [ '2022-7-1', '2022-9-30' ],
 *       '2022Q4': [ '2022-10-1', '2022-12-31' ],
 *       '2023Q1': [ '2023-1-1', '2023-3-31' ],
 *       '2023Q2': [ '2023-4-1', '2023-6-30' ]
 * }
 *
 */
function getQuarter() {
  const data = new Date();
  const curYear = data.getFullYear();
  const curMonth = data.getMonth() + 1;

  let res = {};

  res["2021Q4"] = ["2021-10-1", "2021-12-31"];

  for (let i = 2022; i <= curYear; i++) {
    let quarterList = [];
    if (i === curYear) {
      quarterList = getQuarterListByMonth(curMonth);
    } else {
      quarterList = ["Q1", "Q2", "Q3", "Q4"];
    }
    for (let qStr of quarterList) {
      let { q, span } = getQuarterObj(i, qStr);
      res[q] = span;
    }
  }

  return res;
}

/**
 * 返回季度对象
 * @param {number} year 年份
 * @param {'Q1'|'Q2'|'Q3'|'Q4'} qStr 季度
 * @return {{q:string,span:Array<string>}}
 */
function getQuarterObj(year, qStr) {
  let res = {};
  switch (qStr) {
    case "Q1":
      res = { q: [`${year}Q1`], span: [`${year}-1-1`, `${year}-3-31`] };
      break;
    case "Q2":
      res = { q: [`${year}Q2`], span: [`${year}-4-1`, `${year}-6-30`] };
      break;
    case "Q3":
      res = { q: [`${year}Q3`], span: [`${year}-7-1`, `${year}-9-30`] };
      break;
    case "Q4":
      res = { q: [`${year}Q4`], span: [`${year}-10-1`, `${year}-12-31`] };
      break;
  }
  return res;
}

function getQuarterListByMonth(month) {
  if (month >= 1 && month <= 3) {
    return ["Q1"];
  }
  if (month >= 4 && month <= 6) {
    return ["Q1", "Q2"];
  }
  if (month >= 7 && month <= 9) {
    return ["Q1", "Q2", "Q3"];
  }
  if (month >= 10 && month <= 12) {
    return ["Q1", "Q2", "Q3", "Q4"];
  }
}

module.exports = {
  getQuarterObj,
  getQuarter,
  getDatespanStr,
};
